package com.aya.util;

import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.w3c.dom.Element;

public class UtilNamespaceHandler extends NamespaceHandlerSupport {


    @Override
    public void init() {
        registerBeanDefinitionParser("list", new UtilListBeanDefinitionParser());

    }

    class UtilListBeanDefinitionParser extends AbstractBeanDefinitionParser {
        @Override
        protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
            String genericClass = element.getAttribute("class");
            Assert.hasText(genericClass, "class must not be null");
            BeanDefinitionBuilder builder = BeanDefinitionBuilder.
                    genericBeanDefinition(ListFactoryBean.class);
            builder.addPropertyValue("genericClass", genericClass);
            builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
            return builder.getBeanDefinition();

        }


    }

}
